<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 商品模块
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-05-27 11:31:33 +0800 (二, 2014-05-27) $
 * @version : $Id: goods.php 107 2014-05-27 03:31:33Z spike $
 * @rev     : $Revision: 107 $
 */
class Goods extends MY_Controller 
{
    public function __construct()
    {
        parent::__construct();
    }

    public function attr()
    {
        $params = $this->input->get();

        $config   = $this->config->item('attr_img');
        $attr_img = $config[$params['attr']];
        if( ! $attr_img )
        {
            show_error("无法找到家族。");
        }
        $title_img   = IMG_DOMAIN.'titles/head_4.gif';

        $data = array(
            'current_url'=> DOMAIN."goods/attr/?attr={$params['attr']}",
            'title_img'  => $title_img,
            'attr_img'   => $attr_img,
            'attr'       => $params['attr'],
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('goods/attr.tpl');
        $this->load->view('home/footer.tpl');

    }

    public function search()
    {
        $params = $this->input->get();

        $this->load->model('Goods_model');
        $list = $this->Goods_model->browse_goods($params, $params['page'], DEFAULT_PAGESIZE, $params['orderby']);
        
        $this->load->dao('Category_dao');
        $category = $this->Category_dao->get_one(
            array(
                'seo_name'  => $params['cat'], 
                'is_delete' => UNDELETED, 
                'status'    => CATEGORY_STATUS_ON
            )
        );
        // 绑定分类图片
        if( ! empty($params['cat']) )
        {
            $config      = $this->config->item('cat_title_img');
            $title_img   = $config[$category['category_id']];
        }

        if( ! empty($params['attr']) )
        {
            $title_img   = IMG_DOMAIN.'titles/head_4.gif';
        }

        $this->load->library('pagination');
        $config['total_rows'] = $list['total'];
        $config['per_page']   = $list['size'];

        $this->pagination->initialize($config); 
        $pagination = $this->pagination->create_links();

        $data = array(
            'pagination' => $pagination,
            'default'    => $params ? $params : array(),
            'total'      => $total,
            'list'       => $list,
            'current_url'=> $this->_build_url($params),
            'title_img'  => $title_img,
            'attr_img'   => $attr_img,
            'query'      => $params ? http_build_query($params) : '',
            'orderby'    => array(
                            '价格'=>'price_desc', 
                            '新品'=>'new_desc',
                            '人气'=>'popular_desc'
                        ),
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('goods/search.tpl');
        $this->load->view('home/footer.tpl');
    }

    public function new_release()
    {
        $params = $this->input->get();

        $this->load->model('Goods_model');
        $list = $this->Goods_model->browse_goods($params, $params['page'], 8, 'new_desc');

        $this->load->library('pagination');
        $config['total_rows'] = $list['total'];
        $config['per_page']   = $list['size'];

        $this->pagination->initialize($config); 
        $pagination = $this->pagination->create_links();

        $data = array(
            'pagination' => $pagination,
            'default'    => $params ? $params : array(),
            'total'      => $total,
            'list'       => $list,
            'title_img'  => IMG_DOMAIN.'titles/head_1.gif',
            'current_url'=> DOMAIN.'goods/new_release',
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('goods/search.tpl');
        $this->load->view('home/footer.tpl');
    }

    public function most_popular()
    {
        $params = $this->input->get();

        $this->load->model('Goods_model');
        $list = $this->Goods_model->browse_goods($params, $params['page'], DEFAULT_PAGESIZE, 'popular_desc');

        $this->load->library('pagination');
        $config['total_rows'] = $list['total'];
        $config['per_page']   = $list['size'];

        $this->pagination->initialize($config); 
        $pagination = $this->pagination->create_links();

        $data = array(
            'pagination' => $pagination,
            'default'    => $params ? $params : array(),
            'total'      => $total,
            'list'       => $list,
            'title_img'  => IMG_DOMAIN.'titles/head_2.gif',
            'current_url'=> DOMAIN.'goods/most_popular',
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('goods/search.tpl');
        $this->load->view('home/footer.tpl');
    }

    protected function _build_url($params)
    {
        $url = DOMAIN.'goods/search/?';
        if($params['cat'])
        {
            $url .= "cat={$params['cat']}&";
        }

        if($params['attr'])
        {
            $url .= "attr={$params['attr']}&";
        }

        if($params['name'])
        {
            $url .= "name={$params['name']}&";
        }

        if($params['orderby'])
        {
            $url .= "orderby={$params['orderby']}&";
        }

        if($params['page'])
        {
            $url .= "page={$params['page']}&";
        }

        return rtrim($url, '&');
    }

    public function view($goods_sn = NULL)
    {
        $this->load->model('Goods_model');
        if( ! $goods = $this->Goods_model->get_goods($goods_sn, TRUE, TRUE, TRUE))
        {
            show_error("无法找到商品。");
        }

        $this->Goods_model->update_popular($goods_sn);

        $data  = array(
            'goods'        => $goods,
            'max_img'      => Goods_model::$MAX_IMG,
            'web_keywords' => $goods['name']. ', '.WEB_KEYWORDS,
            'web_desc'     => $goods['name'].' - '.WEB_DESC,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('goods/view.tpl');
        $this->load->view('home/footer.tpl');
    }
}